<template>
  <div>
    <!-- 已驳回 4 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="通过剩余关"
      :visible="open"
      width="500px"
      append-to-body
      @close="cancel()"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="type == 1 || type == 2 ? rules : {}"
        label-width="40px"
      >
        <el-form-item label="原因" prop="reason">
          <el-input
            v-if="type == 1 || type == 2"
            maxlength="100"
            show-word-limit
            :rows="3"
            v-model="form.reason"
            type="textarea"
            placeholder="请输入原因"
          />
          <div v-else style="white-space: pre-wrap">{{ form.reason }}</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick @click="cancel">取 消</el-button>
        <el-button
          v-preventReClick
          type="primary"
          @click="submitForm"
          v-if="type == 1 || type == 2"
        >确 定
        </el-button>
        <el-button
          v-preventReClick
          type="primary"
          @click="cancel"
          v-if="type == 3"
        >确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { finishCourse } from '@/api/home'

export default {
  props: {
    open: Boolean,
    type: [Number, String],
    courseSchedulingId: [Number, String],
    workstationId: [Number, String],
    studentId: [Number, String],
    courseId: [Number, String]
  },
  data() {
    return {
      // 表单参数
      form: {
        reason: undefined
      },
      // 表单校验
      rules: {
        //   taskName: [
        //     { required: true, message: "步骤名称不能为空", trigger: "blur" },
        //   ],
      }
    }
  },
  watch: {
    open(e) {
      if (e) {
        if (this.type == 1) {
          this.reset()
        }
      }
    }
  },
  mounted() {
  },
  methods: {
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$modal
            .confirm('是否确定通过剩余关')
            .then(() => {
              let formData = new FormData()
              formData.append('reason', this.form.reason)
              formData.append('courseSchedulingId', this.courseSchedulingId)
              formData.append('studentId', this.studentId)
              formData.append('courseId', this.courseId)
              formData.append('workstationId', this.workstationId)
              finishCourse(formData).then(() => {
                this.$emit('update:open', false)
                this.$emit('refresh', false)
                this.$modal.msgSuccess('操作成功')
              })
            })
            .catch(() => {
            })
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        reason: undefined
      }
      this.resetForm('form')
    },
    cancel() {
      this.$emit('update:open', false)
    }
  }
}
</script>
<style scoped lang="scss"></style>
